package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.ApCollectionService;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements ApCollectionService {

    /**
     * 文章收藏
     * @param dto
     * @return
     */
    public ResponseResult collection(CollectionBehaviorDto dto){
        if (dto.getEntryId()==null||dto.getOperation()==null||dto.getType()==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getType()<0||dto.getType()>1||dto.getOperation()<0||dto.getOperation()>1){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser user = AppThreadLocalUtil.getUser();
        QueryWrapper<ApCollection> wrapper=new QueryWrapper<>();
        if (dto.getOperation()==0){
            //收藏
            //查看是否已收藏
            if (isCollection(dto.getEntryId(),user.getId())){
                System.out.println("已收藏");
                throw new CustomException(AppHttpCodeEnum.HAS_COLLECTION);
            }

            ApCollection collection=new ApCollection();
            collection.setCollectionTime(new Date());
            collection.setType(dto.getType());
            collection.setArticleId(dto.getEntryId());
            collection.setEntryId(user.getId());
            collection.setPublishedTime(dto.getPublishedTime());
            boolean save = save(collection);
            if (!save){
                throw new CustomException(AppHttpCodeEnum.FAIL_TO_SAVE_COLLECTION);
            }
        }
        if (dto.getOperation()==1){
            //取消收藏
            wrapper.eq("entry_id",user.getId());
            wrapper.eq("article_id",dto.getEntryId());
            boolean remove = remove(wrapper);
            if (!remove){
                throw new CustomException(AppHttpCodeEnum.FAIL_TO_CANCEL_COLLECTION);
            }
        }

        return new ResponseResult<>();
    }

    /**
     * 是否已收藏
     * @param articleId
     * @param userId
     * @return
     */
    public boolean isCollection(Long articleId,Integer userId){
        QueryWrapper<ApCollection> wrapper=new QueryWrapper<>();
        wrapper.eq("entry_id",userId);
        wrapper.eq("article_id",articleId);
        ApCollection one = getOne(wrapper);

        if (one!=null){
            return true;
        }
        return false;
    }

}
